package i2.application.checklist.entities;

import static javax.persistence.GenerationType.AUTO;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Entity
@Table(name = "checklist")
@NamedQuery(name = "CheckList.findWithCompleteLivraison", query = "FROM CheckList c LEFT OUTER JOIN FETCH c.livraison.livrables WHERE c.id = :id")
public class CheckList implements Serializable {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = LoggerFactory.getLogger(CheckList.class);

	@Id
	@GeneratedValue(strategy = AUTO)
	@Column(name = "id", unique = true, nullable = false)
	private int id;

	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "livraison_id", nullable = false)
	@NotNull
	private Livraison livraison;
	
	@OneToMany(mappedBy = "checkList", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	private List<Reponse> reponseList = new ArrayList<Reponse>();
	
	@Column(name = "done", nullable = false)
	private Boolean done = false;
	
	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "rapport_id")
	private Rapport rapport;
	
	public CheckList() {
	}

	public CheckList(int id) {
		this.id = id;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public Livraison getLivraison() {
		return livraison;
	}

	public void setLivraison(Livraison livraison) {
		this.livraison = livraison;
	}

	public List<Reponse> getReponseList() {
		return reponseList;
	}

	public void setReponseList(List<Reponse> reponseList) {
		this.reponseList = reponseList;
	}
	
	public boolean isDone() {
		return done;
	}

	public void setDone(boolean done) {
		this.done = done;
	}
	
	public Rapport getRapport() {
		return rapport;
	}

	public void setRapport(Rapport rapport) {
		this.rapport = rapport;
	}

	public void addReponse(Reponse reponse) {
		reponseList.add(reponse);
		reponse.setCheckList(this);
	}
	
	public Map<Categorie, List<Reponse>> getTreeReponses() {
		logger.debug("begin getTreeReponses");
		Map<Categorie, List<Reponse>> treeReponse = new HashMap<Categorie, List<Reponse>>();
		for (Reponse reponse : reponseList) {
			if (!treeReponse.containsKey(reponse.getQuestion().getCategorie())) {
				treeReponse.put(reponse.getQuestion().getCategorie(), new ArrayList<Reponse>());
			}
			treeReponse.get(reponse.getQuestion().getCategorie()).add(reponse);
		}
		return treeReponse;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		CheckList other = (CheckList) obj;
		if (id != other.id) {
			return false;
		}
		return true;
	}
	
	@Override
	public String toString() {
		return "CheckList [id=" + id + ", nb reponse="+reponseList.size()+"]";
	}
	
}
